﻿/*********************************************************************************
 *Author:         OnClick
 *Version:        1.0
 *UnityVersion:   2018.3.11f1
 *Date:           2019-04-06
 *Description:    IFramework
 *History:        2018.11--
*********************************************************************************/
using System.Collections.Generic;
using UnityEngine;

namespace IFramework
{
    public class ABBundle
	{
        public int ReferenceCount;
        public virtual string Err { get; protected set; }
        public virtual float Progress { get; protected set; }
        public virtual bool IsDone { get; protected set; }
        public string Path { get; protected set; }
        public string ABName { get; internal set; }
        public virtual AssetBundle AB { get { return ab; } }
        public readonly List<ABBundle> dpBundles = new List<ABBundle>();
        private AssetBundle ab;
        protected Hash128 version;
        public ABBundle(string url, Hash128 version)
        {
            Path = url;
            this.version = version;
        }
        public void Load() { OnLoad(); }
        protected virtual void OnLoad() {
            ab = AssetBundle.LoadFromFile(Path);
            if (ab == null) Err = Path + " LoadFromFile failed.";
        }
        public void UnLoad() { OnUnLoad(); }
        protected virtual void OnUnLoad() {
            if (ab == null) return;
            ab.Unload(true);
            ab = null;
        }
        public T LoadAsset<T>(string assetName) where T : Object
        {
            if (Err != null) return null;
            if (ab == null) return null;
            return ab.LoadAsset(assetName, typeof(T)) as T;
        }
        public Object LoadAsset(string assetName, System.Type assetType)
        {
            if (Err != null) return null;
            return AB.LoadAsset(assetName, assetType);
        }
        public AssetBundleRequest LoadAssetAsync(string assetName, System.Type assetType)
        {
            if (Err != null) return null;
            if (AB == null) return null;
            return AB.LoadAssetAsync(assetName, assetType);
        }
        public void Release()
        {
            if (--ReferenceCount < 0)
            {
               Log.E("ABBundle : refCount < 0");
            }
        }
        public void Retain()
        {
            ReferenceCount++;
        }
    }
 
}
